#include "rcc_drv.h"

void RCC_Configuration(void)
{
    //----------使用外部RC晶振-----------
    RCC_DeInit();              // 初始化为缺省值
    RCC_HSEConfig(RCC_HSE_ON); // 使能外部的高速时钟
    while (RCC_WaitForHSEStartUp() != SUCCESS); // 等待外部高速时钟使能就绪
    
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); // Enable Prefetch Buffer
    FLASH_SetLatency(FLASH_Latency_2);                    // Flash 2 wait state

    RCC_HCLKConfig(RCC_SYSCLK_Div1); // HCLK = SYSCLK
    RCC_PCLK1Config(RCC_HCLK_Div2);  // PCLK1 = HCLK/2
    RCC_PCLK2Config(RCC_HCLK_Div1);  // PCLK2 =  HCLK

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); // PLLCLK = 8MHZ * 9 =72MHZ
    RCC_PLLCmd(ENABLE);                                  // Enable PLLCLK

    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);           // Wait till PLLCLK is ready
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Select PLL as system clock
    while (RCC_GetSYSCLKSource() != 0x08); // Wait till PLL is used as system clock source

    //---------打开相应外设时钟--------------------
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOE, ENABLE); // 使能APB2外设的GPIOA、GPIOB、GPIOE的时钟
}
